import numpy as np
import time

def partial_pivot(A, b):
    n = len(b)

    start_time = time.time()

    for k in range(n - 1):
        # 部分主元选取：选择列元素绝对值最大的行，交换到第一行
        max_index = np.argmax(np.abs(A[k:, k])) + k
        A[[k, max_index]] = A[[max_index, k]]
        b[[k, max_index]] = b[[max_index, k]]

        # 前向消元
        for i in range(k + 1, n):
            multiplier = -A[i, k] / A[k, k]
            A[i, k:] += multiplier * A[k, k:]
            b[i] += multiplier * b[k]

    # 回代求解
    x = np.zeros(n)
    for i in range(n - 1, -1, -1):
        x[i] = (b[i] - np.dot(A[i, i+1:], x[i+1:])) / A[i, i]

    end_time = time.time()
    execution_time = end_time - start_time

    return x, execution_time


# 使用示例
n = 8000
A = np.random.rand(n, n)
b = np.random.rand(n)

x, execution_time = partial_pivot(A, b)
print("解向量 x:", x)
print("执行时间:", execution_time, "秒")
